﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Sys.Serialization;

namespace Sys.SqlServer.Smo
{
    public class Script
    {
        public Guid ID { get; set; }

        public string Name { get; set; }

        public DateTime Timestamp { get; set; }

        public string Author { get; set; }

        public string AuthoringTool { get; set; }

        public List<Scriptlet> Data { get; set; }

        public string Remarks { get; set; }

        public string Hash { get; set; }

        public bool IsHashed { get; set; }

        public bool IsEncrypted { get; set; }

        public Script()
        {
            ID = Guid.NewGuid();
            Name = "Untitled";
            Timestamp = DateTime.Now;
            Author = string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName);
            AuthoringTool = string.Empty;
            Data = new List<Scriptlet>();
            Remarks = string.Empty;
            Hash = string.Empty;
            IsHashed = false;
            IsEncrypted = false;
        }

        public void Load(FileInfo file)
        {
            Script s = Serializer<Script>.Current.DeserializeFromFile(file);

            if (s != null)
            {
                ID = s.ID;
                Name = s.Name;
                Timestamp = s.Timestamp;
                Author = s.Author;
                AuthoringTool = s.AuthoringTool;
                Data = s.Data;
                Remarks = s.Remarks;
                Hash = s.Hash;
                IsHashed = s.IsHashed;
                IsEncrypted = s.IsEncrypted;
            }
        }

        public void Save(FileInfo file)
        {
            Serializer<Script>.Current.SerializeToFile(this, file);
        }
    }
}